/**
 * 题目选择器（单选、多选）
 * 	只显示没有指导教师的题目
 */
var SubjectSelector = {

	/**
	 * callbackOrConf 回调函数或配置选项，若为map类型，则表示为配置选项 isSingle 是否单选
	 */
	getView : function(callbackOrConf, isSingle, selval) {
		// 单选
		if (typeof(callbackOrConf) == 'object') {
			this.scope = callbackOrConf.scope || this;
			this.callback = callbackOrConf.callback;
		} else {
			this.scope = this;
			this.callback = callbackOrConf;
		}
		this.isSingle = (isSingle != null) ? isSingle : true;

		var panel = this.initPanel(isSingle, selval);

		this.win = new Ext.Window({
					title : '选择题目',
					iconCls : 'menu-appuser',
					width : 900,
					minWidth : 900,
					height : 600,
					minHeight : 600,
					layout : 'fit',
					border : false,
					maximizable : true,
					modal : true,
					items : [panel],
					buttonAlign : 'center',
					buttons : [{
								text : '确认',
								iconCls : 'btn-ok',
								scope : this,
								handler : this.submit
							}, {
								text : '关闭',
								iconCls : 'btn-cancel',
								scope : this,
								handler : this.close
							}]
				});
		return this.win;
	},
	getView2 : function(callbackOrConf, isSingle, selval) {
		// 单选
		if (typeof(callbackOrConf) == 'object') {
			this.scope = callbackOrConf.scope || this;
			this.callback = callbackOrConf.callback;
		} else {
			this.scope = this;
			this.callback = callbackOrConf;
		}
		this.isSingle = (isSingle != null) ? isSingle : true;

		var panel = this.init2Panel(isSingle, selval);

		this.win = new Ext.Window({
					title : '选择题目',
					iconCls : 'menu-appuser',
					width : 900,
					minWidth : 900,
					height : 600,
					minHeight : 600,
					layout : 'fit',
					border : false,
					maximizable : true,
					modal : true,
					items : [panel],
					buttonAlign : 'center',
					buttons : [{
								text : '确认',
								iconCls : 'btn-ok',
								scope : this,
								handler : this.submit
							}, {
								text : '关闭',
								iconCls : 'btn-cancel',
								scope : this,
								handler : this.close
							}]
				});
		return this.win;
	},
	/**
	 * 组件初始化 isSingle：是否单选,默认单选
	 */
	init2Panel : function(isSingle, selval) {
		this.searchPanel = new KE.SearchPanel({
					region : 'north',
					items : [{
								text : '题目名称'
							}, {
								width : 120,
								name : 'Q_LIKES_name',
								xtype : "textfield"
							}, {
								text : '指导教师'
							}, {
								width : 120,
								name : 'Q_LIKES_teacher.fullname',
								xtype : "textfield"
							}, {
								xtype : 'button',
								text : '查询',
								scope : this,
								iconCls : 'btn-search',
								handler : this.search
							}, {
								xtype : 'button',
								text : '清空',
								scope : this,
								iconCls : 'reset',
								handler : this.reset
							}]
				});
		// ///////////////gridPanel[全部列表] start///////////////////
		this.gridPanel = new KE.GridPanel({
					title : '题目列表',
					region : 'center',
					isShowTbar : false,
					singleSelect : isSingle,
					autoWidth : true,
					sorters: [{
						property: 'teacher.fullname',
						direction: 'ASC'
					}],
					url : __ctxPath + '/byxt/listNoSubject.do',
					fields : ['oid', 'name', {
							name: 'teacher.fullname',
							mapping: 'teacher.fullname'
						}],
					rownumbercfg : {
						width : 30
					},
					defaults : {
						menuDisabled : true
					},
					columns : [{
								header : "题目",
								dataIndex : 'name',
								flex: 2
							}, {
								header : "指导教师",
								dataIndex : 'teacher.fullname'
							}]
				});
		// 单选：根据传入值，选中GridPanel
		Ext.defer(function() {
					if (isSingle && selval) {
						var model = this.gridPanel.getSelectionModel();
						this.gridPanel.getStore().each(function(rec) {
									if (rec.data.oid == selval) {
										model.select([rec]);
										return;
									}
								});
					}
				}, 200, this);
		// ////////////////////gridPanel[全部列表] end///////////////////////

		// ////////////////////selectedUserGrid[已选列表]
		var me = this;
		this.selectedUserGrid = new KE.GridPanel({
					title : '已选题目',
					border : false,
					isShowTbar : false,
					showPaging : false,
					rownumbercfg : {
						width : 30
					},
					store : new Ext.data.ArrayStore({
								fields : ['oid', 'name']
							}),
					columns : [{
								header : "题目",
								dataIndex : 'name',
								flex: 2						
							}]
				});
		this.selectedUserGrid.on('rowdblclick', function(grid, rec) {
					var grid = me.selectedUserGrid;
					var store = grid.getStore();
					var rows = grid.getSelectionModel().getSelection();
					for (var i = 0; i < rows.length; i++) {
						store.remove(rows[i]);
					}
				});
		// ///////////////////selectedUserGrid[已选列表] end //////////////
		this.gridPanel.on('rowdblclick', function(grid, rec) {
					var selGrid = me.selectedUserGrid;
					var selStore = selGrid.getStore();
					var rows = me.gridPanel.getSelectionModel().getSelection();
					for (var i = 0; i < rows.length; i++) {
						var oid = rows[i].data.oid;
						var name = rows[i].data.name;
						var isExist = false;
						// 查找是否存在该记录
						for (var j = 0; j < selStore.getCount(); j++) {
							if (selStore.getAt(j).data.oid == oid) {
								isExist = true;
								break;
							}
						}
						if (!isExist) {
							var newData = {
								oid : oid,
								name : name
							};
							var rec = selStore.getModel().create(newData);
							selStore.insert(selStore.getCount(), rec);
						}
					}
				});
		// 多选：根据传入值，选中GridPanel
		Ext.defer(function() {
					if (!isSingle && selval) {
						$postAjax({
									url : __ctxPath + '/byxt/selectedSubject.do',
									params : {
										ids : selval
									},
									callback : function(json) {
										if (json.length > 0) {
											for (var i = 0; i < json.length; i++) {
												var newData = {
													oid : json[i].oid,
													name : json[i].name
												};
												var selStore = me.selectedUserGrid.getStore();
												var rec = selStore.getModel().create(newData);
												selStore.insert(selStore.getCount(), rec);
											}
										}
									}
								});
					}
				}, 300);

		// ///////////////////selectedPanel[多选面板] start/////////////////////
		// 多选添加的面板
		this.selectedPanel = new Ext.Panel({
					layout : 'border',
					region : 'east',
					width : 350,
					border : false,
					items : [new Ext.Panel({
										region : 'west',
										width : 50,
										layout : {
											type : 'vbox',
											pack : 'center',
											align : 'stretch'
										},
										defaults : {
											margin : '0 5 10 5',
											xtype : 'button'
										},
										// defaultType : 'button',
										items : [{
													iconCls : 'add-all',
													text : '',
													scope : this,
													handler : this.addAll
												}, {
													iconCls : 'rem-all',
													text : '',
													scope : this,
													handler : this.removeAll
												}]
									}), {
								region : 'center',
								layout : 'fit',
								items : [this.selectedUserGrid]
							}]
				}); // selectedPanel
		// /////////////////////selectedPanel end//////////////////////////////

		var panel = new Ext.Panel({
					id : 'contactPanel',
					layout : 'border',
					anchor : '96%,96%',
					items : [this.searchPanel, this.gridPanel]
				}); // end of this contactPanel
		// 添加：多选面板
		if (isSingle != null && isSingle == false) {
			panel.add(this.selectedPanel);
			panel.doLayout();
		}
		return panel;
	},
	/**
	 * 组件初始化 isSingle：是否单选,默认单选
	 */
	initPanel : function(isSingle, selval) {
		this.searchPanel = new KE.SearchPanel({
					region : 'north',
					items : [{
								text : '题目名称'
							}, {
								width : 120,
								name : 'Q_LIKES_name',
								xtype : "textfield"
							}, {
								text : '指导教师'
							}, {
								width : 120,
								name : 'Q_LIKES_teacher.fullname',
								xtype : "textfield"
							}, {
								xtype : 'button',
								text : '查询',
								scope : this,
								iconCls : 'btn-search',
								handler : this.search
							}, {
								xtype : 'button',
								text : '清空',
								scope : this,
								iconCls : 'reset',
								handler : this.reset
							}]
				});
		// ///////////////gridPanel[全部列表] start///////////////////
		this.gridPanel = new KE.GridPanel({
					title : '题目列表',
					region : 'center',
					isShowTbar : false,
					singleSelect : isSingle,
					autoWidth : true,
					sorters: [{
						property: 'teacher.fullname',
						direction: 'ASC'
					}],
					url : __ctxPath + '/byxt/listUnSubject.do',
					fields : ['oid', 'name', {
							name: 'teacher.fullname',
							mapping: 'teacher.fullname'
						}],
					rownumbercfg : {
						width : 30
					},
					defaults : {
						menuDisabled : true
					},
					columns : [{
								header : "题目",
								dataIndex : 'name',
								flex: 2
							}, {
								header : "指导教师",
								dataIndex : 'teacher.fullname'
							}]
				});
		// 单选：根据传入值，选中GridPanel
		Ext.defer(function() {
					if (isSingle && selval) {
						var model = this.gridPanel.getSelectionModel();
						this.gridPanel.getStore().each(function(rec) {
									if (rec.data.oid == selval) {
										model.select([rec]);
										return;
									}
								});
					}
				}, 200, this);
		// ////////////////////gridPanel[全部列表] end///////////////////////

		// ////////////////////selectedUserGrid[已选列表]
		var me = this;
		this.selectedUserGrid = new KE.GridPanel({
					title : '已选题目',
					border : false,
					isShowTbar : false,
					showPaging : false,
					rownumbercfg : {
						width : 30
					},
					store : new Ext.data.ArrayStore({
								fields : ['oid', 'name']
							}),
					columns : [{
								header : "题目",
								dataIndex : 'name',
								flex: 2						
							}]
				});
		this.selectedUserGrid.on('rowdblclick', function(grid, rec) {
					var grid = me.selectedUserGrid;
					var store = grid.getStore();
					var rows = grid.getSelectionModel().getSelection();
					for (var i = 0; i < rows.length; i++) {
						store.remove(rows[i]);
					}
				});
		// ///////////////////selectedUserGrid[已选列表] end //////////////
		this.gridPanel.on('rowdblclick', function(grid, rec) {
					var selGrid = me.selectedUserGrid;
					var selStore = selGrid.getStore();
					var rows = me.gridPanel.getSelectionModel().getSelection();
					for (var i = 0; i < rows.length; i++) {
						var oid = rows[i].data.oid;
						var name = rows[i].data.name;
						var isExist = false;
						// 查找是否存在该记录
						for (var j = 0; j < selStore.getCount(); j++) {
							if (selStore.getAt(j).data.oid == oid) {
								isExist = true;
								break;
							}
						}
						if (!isExist) {
							var newData = {
								oid : oid,
								name : name
							};
							var rec = selStore.getModel().create(newData);
							selStore.insert(selStore.getCount(), rec);
						}
					}
				});
		// 多选：根据传入值，选中GridPanel
		Ext.defer(function() {
					if (!isSingle && selval) {
						$postAjax({
									url : __ctxPath + '/byxt/selectedSubject.do',
									params : {
										ids : selval
									},
									callback : function(json) {
										if (json.length > 0) {
											for (var i = 0; i < json.length; i++) {
												var newData = {
													oid : json[i].oid,
													name : json[i].name
												};
												var selStore = me.selectedUserGrid.getStore();
												var rec = selStore.getModel().create(newData);
												selStore.insert(selStore.getCount(), rec);
											}
										}
									}
								});
					}
				}, 300);

		// ///////////////////selectedPanel[多选面板] start/////////////////////
		// 多选添加的面板
		this.selectedPanel = new Ext.Panel({
					layout : 'border',
					region : 'east',
					width : 350,
					border : false,
					items : [new Ext.Panel({
										region : 'west',
										width : 50,
										layout : {
											type : 'vbox',
											pack : 'center',
											align : 'stretch'
										},
										defaults : {
											margin : '0 5 10 5',
											xtype : 'button'
										},
										// defaultType : 'button',
										items : [{
													iconCls : 'add-all',
													text : '',
													scope : this,
													handler : this.addAll
												}, {
													iconCls : 'rem-all',
													text : '',
													scope : this,
													handler : this.removeAll
												}]
									}), {
								region : 'center',
								layout : 'fit',
								items : [this.selectedUserGrid]
							}]
				}); // selectedPanel
		// /////////////////////selectedPanel end//////////////////////////////

		var panel = new Ext.Panel({
					id : 'contactPanel',
					layout : 'border',
					anchor : '96%,96%',
					items : [this.searchPanel, this.gridPanel]
				}); // end of this contactPanel
		// 添加：多选面板
		if (isSingle != null && isSingle == false) {
			panel.add(this.selectedPanel);
			panel.doLayout();
		}
		return panel;
	}, // init

	// //////////////###方法###///////////////////////

	// 添加所有
	addAll : function() {
		var gridPanel = this.gridPanel;
		var selGrid = this.selectedUserGrid;
		var selStore = selGrid.getStore();
		var rows = gridPanel.getSelectionModel().getSelection();
		for (var i = 0; i < rows.length; i++) {
			var oid = rows[i].data.oid;
			var name = rows[i].data.name;
			var isExist = false;
			// 查找是否存在该记录
			for (var j = 0; j < selStore.getCount(); j++) {
				if (selStore.getAt(j).data.oid == oid) {
					isExist = true;
					break;
				}
			}
			if (!isExist) {
				var newData = {
					oid : oid,
					name : name
				};
				var rec = selStore.getModel().create(newData);
				selStore.insert(selStore.getCount(), rec);
			}
		}
		selGrid.getView().refresh();
	},

	// 移除所有
	removeAll : function() {
		var selGrid = this.selectedUserGrid;
		var rows = selGrid.getSelectionModel().getSelection();
		var selStore = selGrid.getStore();
		for (var i = 0; i < rows.length; i++) {
			selStore.remove(rows[i]);
		}
	},

	/**
	 * 确定，提交
	 * 
	 * @param isSingle
	 *            是否单选
	 * @param callback
	 *            回传函数
	 */
	submit : function() {
		var ids = '';
		var names = '';
		if (this.isSingle == null || this.isSingle) {// 选择单个用户
			var grid = this.gridPanel;
			var rows = grid.getSelectionModel().getSelection();

			for (var i = 0; i < rows.length; i++) {
				if (i > 0) {
					ids += ',';
					names += '，';
				}
				ids += rows[i].data.oid;
				names += rows[i].data.name;
			}
		} else {
			var selStore = this.selectedUserGrid.getStore();
			for (var i = 0; i < selStore.getCount(); i++) {
				if (i > 0) {
					ids += ',';
					names += '，';
				}
				ids += selStore.getAt(i).data.oid;
				names += selStore.getAt(i).data.name;
			}
		}

		if (this.callback != null) {
			this.callback.call(this.scope, ids, names);
		}
		this.win.close();
	},

	/**
	 * 关闭当前窗口
	 */
	close : function() {
		this.win.close();
	},
	// 重置查询表单
	reset : function() {
		this.searchPanel.getForm().reset();
	},
	// 按条件搜索
	search : function() {
		$search({
					searchPanel : this.searchPanel,
					gridPanel : this.gridPanel
				});
	}
};
